<div data-role="view" data-title="Maybanhang.Net" id="summary" data-layout="mobile-view">
    <header data-role="header">
        <div data-role="navbar">
            <a id="back-button" class="nav-button" data-align="left" data-role="backbutton">Back</a>
            <!--<span data-role="view-title"></span>-->
            <span ><?php echo UserInformation::getLocationName(); ?></span>
            <a data-align="right" data-role="button" class="nav-button" href='#index'>Home</a>            
        </div>
    </header>
    <div class="chart-wrapper">
                <div id="chart"></div>
    </div>
    <script>
    			<?php
    				$lastMonday = strtotime('monday this week');
					$dtStr = date("c", $lastMonday);
					$date = new DateTime($dtStr);
					$refDate = new DateTime($dtStr);
					$dateArray = "var dates = [";
					$dateArray = $dateArray."'".date_format($date,"m-d")."',";
					date_add($date,date_interval_create_from_date_string("1 days"));
					$dateArray = $dateArray."'".date_format($date,"m-d")."',";
					date_add($date,date_interval_create_from_date_string("1 days"));
					$dateArray = $dateArray."'".date_format($date,"m-d")."',";
					date_add($date,date_interval_create_from_date_string("1 days"));
					$dateArray = $dateArray."'".date_format($date,"m-d")."',";
					date_add($date,date_interval_create_from_date_string("1 days"));
					$dateArray = $dateArray."'".date_format($date,"m-d")."',";
					date_add($date,date_interval_create_from_date_string("1 days"));
					$dateArray = $dateArray."'".date_format($date,"m-d")."',";
					date_add($date,date_interval_create_from_date_string("1 days"));
					$dateArray = $dateArray."'".date_format($date,"m-d")."'];";
					date_add($date,date_interval_create_from_date_string("1 days"));
					echo $dateArray;
					
					$ezRestApi = new EzRestApi();	
					$accountId = UserInformation::getAccountId();
					$locationId = UserInformation::getLocationId();	
					$array = array(0=>"0", 1=>"0", 2=>"0", 3=>"0", 4=>"0", 5=>"0", 6=>"0");
					$summaries=$ezRestApi->getSummaryPurchase($accountId, $locationId);
						reset($summaries);
						while(list(,$value)=each($summaries)) {
							$invoiceDate = new DateTime($value['invoiceDate']);
							$interval = date_diff($invoiceDate, $refDate)->d;
							$array[$interval] = $value['amount'];												
						}
					
					echo "var data = ['".$array[0]."','".$array[1]."','".$array[2]."','".$array[3]."','".$array[4]."','".$array[5]."','".$array[6]."'];";
    			?>
                function createChart() {
                    $("#chart").kendoChart({
                        series: [{
                            type: "column",
                            data: data,
                            aggregate: "avg"
                        }],
                        categoryAxis: {
                            baseUnit: "days",
                            categories: dates,
                            majorGridLines: {
                                visible: false
                            }
                        },
                        valueAxis: {
                            line: {
                                visible: false
                            }
                        }
                    });
                }

                $(document).ready(function() {
                    setTimeout(function() {
                        // Initialize the chart with a delay to make sure
                        // the initial animation is visible
                        createChart();

                        $("#example").bind("kendo:skinChange", function(e) {
                            createChart();
                        });
                    }, 400);

                    $(".configuration-horizontal").bind("change", refresh);
                });

                function refresh() {
                    var chart = $("#chart").data("kendoChart"),
                        series = chart.options.series,
                        categoryAxis = chart.options.categoryAxis,
                        baseUnitInputs = $("input:radio[name=baseUnit]"),
                        aggregateInputs = $("input:radio[name=aggregate]");

                    for (var i = 0, length = series.length; i < length; i++) {
                        series[i].aggregate = aggregateInputs.filter(":checked").val();
                    };

                    categoryAxis.baseUnit = baseUnitInputs.filter(":checked").val();

                    chart.refresh();
                }
            </script>
            <ul data-role="listview" data-style="inset" data-type="group">
	            <?php
	            	$today = new DateTime();
	            	$summaryToday=$ezRestApi->getSummaryWithDetail($accountId, $locationId,$refDate,$today);
					if ($summaryToday)
					{
						echo '<li>Doanh thu : '.$summaryToday['amount'].'</li>';
						echo '<li>Số hóa đơn: '.$summaryToday['numOfInvoice'].'</li>';
					}
					if ($summaryToday['lastInvoices'])
					{
						echo '<li>Hóa đơn mới';
						echo '<ul>';						
						$lastInvoices = $summaryToday['lastInvoices'];						
						reset($lastInvoices);
						while(list(,$value)=each($lastInvoices)) {
							echo "<li ><a>".$value['invoiceCode']."</a></li>";											
						}
						echo '</ul>';
						echo '</li>';
					}
	            ?>
             </ul>
</div>